﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Messaging;
using ProjectWizardManagementv1.ViewModels.Concrete;
using ProjectWizardManagementv1.ViewModels.Concrete.Message;

namespace ProjectWizardManagementv1.WpfComponents.UserControls
{
    /// <summary>
    /// Interaction logic for NotificationBox.xaml
    /// </summary>
    public partial class NotificationBox : UserControl
    {
        private DoubleAnimation _fadeIn;
        private DoubleAnimation _fadeOut;
        private DispatcherTimer _timer;

        private NotificationBoxMessage _currentNotification;

        private List<NotificationBoxMessage> Notifications { get; set; }

        public NotificationBox()
        {
            InitializeComponent();
            Notifications = new List<NotificationBoxMessage>();
            _fadeIn = new DoubleAnimation {From = 0, To = 1.2, Duration = new Duration(TimeSpan.Parse("0:0:2.0"))};
            _fadeOut = new DoubleAnimation{To=0, Duration = new Duration(TimeSpan.Parse("0:0:1.5"))};

            this.Visibility = Visibility.Visible;
            //Loaded += FadeIn_Begin;
            _fadeIn.Completed += FadeIn_Completed;
            _fadeOut.Completed += FadeOut_Completed;
            _timer = new DispatcherTimer { Interval = TimeSpan.Parse("0:0:10") };
            _timer.Tick += delegate { FadeOut(); };

            Messenger.Default.Register<NotificationBoxMessage>(this, AddNotification);
        }

        private void AddNotification(NotificationBoxMessage notification)
        {
            if (!Notifications.Contains(notification))
            {
                this.Visibility = Visibility.Visible;
                Notifications.Add(notification);

                if(Notifications.Count <= 1)
                FadeIn_Begin(this, new EventArgs());
            }
        }

        public void FadeIn_Begin(object sender, EventArgs e)
        {
            _currentNotification = Notifications[0];
            this.HeaderTitle.Text = _currentNotification.Header;
            this.TextMessage.Text = _currentNotification.Message;
             BeginAnimation(OpacityProperty, _fadeIn);
        }

        private void FadeIn_Completed(object sender, EventArgs e)
        {
                _timer.Start();
        }

        private void FadeOut()
        {
            _timer.Stop();
            BeginAnimation(OpacityProperty, _fadeOut, HandoffBehavior.SnapshotAndReplace);
        }

        private void FadeOut_Completed(object sender, EventArgs e)
        {
            
            Notifications.Remove(_currentNotification);
            if (Notifications.Count >= 1)
                FadeIn_Begin(this, new EventArgs());
            else
                this.Visibility = Visibility.Hidden;   
        }
    }
}
